package com.graduate.run;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;

import java.nio.charset.Charset;

/**
 * 运行方法
 *
 * @createtime 2019-12-28
 * @creator jingchu
 * @finishtime 2020-02-03
 */
@SpringBootApplication(scanBasePackages = "com.graduate")
@EnableAsync//开启异步调用
@MapperScan("com.db.mapper")
public class SpringRun extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(SpringRun.class, args);
    }

    //消息转换器(springboot默认配置了消息转换器编码格式,可以不配)
    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }
    //消息转换器(fastJSON)
    @Bean
    public HttpMessageConverters fastJsonMessageConverter() {
        //创建FastJson的消息转换器
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //创建FastJson的配置对象
        FastJsonConfig config = new FastJsonConfig();
        //对JSON数据进行格式化
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);

        converter.setFastJsonConfig(config);
        HttpMessageConverter<?> converter1 = converter;
        return new HttpMessageConverters(converter1);


    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringRun.class);
    }
}
